/**
 @Name：大学生计算机课程平时成绩管理系统 教师管理
 @Author：dingwen
 */

layui.define(['table'], function (exports) {
    let $ = layui.$
        , table = layui.table
        , setter = layui.setter;


    // 表格数据渲染
    table.render({
        elem: '#LAY-teacher-manage'
        , page: true
        , url: layui.setter.baseUrl + '/teacher/page'
        , toolbar: '#toolbarDemo'
        , title: '教师信息表'
        , parseData: function (res) {
            return {
                "code": 0,
                "msg": res.message,
                "count": res.data.total,
                "data": res.data.records
            };
        }
        , request: {
            pageName: 'pageNum'
            , limitName: 'pageSize'
        }
        , cols:
            [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'teacherId', width: 200, title: '工号', sort: true}
                , {field: 'teacherName', width: 100, title: '姓名', style: 'background-color: #95a5a6; color: #fff;'}
                , {field: 'teacherGender', width: 100, title: '性别', templet: '#teacher-gender'}
                , {field: 'teacherImg', width: 200, title: '头像', templet: '#teacher-img'}
                , {field: 'teacherLevel', width: 100, title: '权限', templet: '#teacher-level'}
                , {field: 'teacherQq', title: 'QQ', minWidth: 200}
                , {field: 'teacherWechat', title: '微信', minWidth: 200}
                , {field: 'teacherPassword', title: '登录密码', templet: '#teacher-pass', width: 100}
                , {
                    title: '操作',
                    width: 200,
                    align: 'center',
                    fixed: 'right',
                    toolbar: '#layuiadmin-app-student-bar'
                }
            ]]
    });
    //监听
    table.on('tool(LAY-teacher-manage)', function (obj) {
        let data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除' + data.teacherName + '老师？', function (index) {
                $.ajax({
                    url: setter.baseUrl + `/teacher/remove/${data.teacherId}`,
                    dataType: "json",
                    type: "delete",
                    contentType: 'application/json',
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg("成功");
                        } else {
                            layer.msg("失败");
                        }
                    },
                    error: function (e) {
                        layer.msg("失败");
                    }
                });
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'reset') {
            layer.confirm('确定重置 ' + data.teacherName + '老师的密码吗？', function (index) {
                let teacher = {
                    teacherId: data.teacherId,
                    teacherPassword: 123456
                };
                $.ajax({
                    url: setter.baseUrl + '/teacher/modify',
                    data: JSON.stringify(teacher),
                    dataType: "json",
                    type: "put",
                    contentType: 'application/json',
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg("重置成功，密码为 123456");
                        } else {
                            layer.msg("失败");
                        }
                    },
                    error: function (e) {
                        layer.msg("失败");
                    }
                });
                layer.close(index);
            });
        }else if (obj.event === 'edit') {

        }
    });
    exports('teacher', {})
});
